package com.fl.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

/*
*   前端跨域过滤器
* */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        //1
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //3
        CorsConfiguration configuration = new CorsConfiguration();
        //放行请求头 CorsConfigurationSource
        configuration.addAllowedHeader("*");
        //不过滤请求方式
        configuration.addAllowedMethod("*");
        //不过滤
        configuration.addAllowedOriginPattern("*");
        configuration.setAllowCredentials(true);
        //2
        source.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(source);
    }
}
